﻿namespace ElisVK
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Reflection;
    using System.Windows;
    using System.Xml.Serialization;
    using ElisVK.Data;
    using ElisVK.Splash;

    using SplashScreen = ElisVK.Splash.SplashScreen;

    class App : Application
    {
        private static SessionInfo _sessionInfo = new SessionInfo();

        private static Settings _settings = null;

        private static readonly string SettingsPath = Path.Combine(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), Assembly.GetExecutingAssembly().GetName().Name), "Settings.xml");

        [STAThread()]
        static void Main()
        {
            Splasher.Splash = new Splash.SplashScreen();
            Splasher.ShowSplash();

            new App();
        }

        public static IWindowUI WindowUI { get; private set; }

        public static Settings Settings
        {
            get
            {
                if (_settings == null)
                {
                    if (File.Exists(SettingsPath))
                    {
                        var deserializer = new XmlSerializer(typeof(Settings));

                        TextReader textReader = new StreamReader(SettingsPath);

                        _settings = (Settings)deserializer.Deserialize(textReader);
                        textReader.Close();
                    }
                    else
                    {
                        _settings = new Settings();
                        _settings.InitializeByDefault();
                        SaveSettings();
                    }
                }
                
                return _settings;   
            }
        }

        public static SessionInfo SessionInfo
        {
            get
            {
                return _sessionInfo;
            }
        }

        public static Assembly SkinAssembly
        {
            get
            {
                var path = Assembly.GetExecutingAssembly().Location;

                path = path.Substring(0, path.LastIndexOf('\\') + 1);

                path = Path.Combine(path ?? string.Empty, @"Skins\BasicSkin" + ".dll");

                return Assembly.LoadFrom(path);
            }
        }

        public App()
        {
            WindowUI = SkinAssembly.CreateInstance("BasicSkin" + ".WindowUI") as IWindowUI;

            if (WindowUI != null)
                Current.Resources.MergedDictionaries.Add(WindowUI.Styles);

            StartupUri = new Uri("MainWindow.xaml", UriKind.Relative);

            Run();
        }

        public static void SaveSettings()
        {
            var serializer = new XmlSerializer(typeof(Settings));
            TextWriter textWriter = new StreamWriter(SettingsPath);
            
            serializer.Serialize(textWriter, _settings);
            textWriter.Close();
        }
    }
}
